package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportProduct;
import cn.itcast.domain.cargo.ExportProductExample;
import cn.itcast.domain.vo.ChartsVo;
import cn.itcast.domain.vo.UserVo;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.utils.BeanMapUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("cargo/export")
public class ExportPdfController extends BaseController {

    @Reference
    ExportService exportService;

    @Reference
    ExportProductService exportProductService;

    @RequestMapping(value="exportPdf",name = "pdf下载")
    public void exportPdf(String id,HttpServletResponse response) throws JRException, IOException {
        //1.加载jasper文件
        InputStream is = session.getServletContext().getResourceAsStream("/jasper/export.jasper");

        //2.准备数据
        //查询报运单
        Export export = exportService.findById(id);

        //查询报运商品
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(id);

        List<ExportProduct> list = exportProductService.findAll(exportProductExample);

        Map map = BeanMapUtils.beanToMap(export);

        //3.填充数据
        //map:填充一个对象,  JRDataSource:填充集合;若集合没有数据使用JREmptyDataSource;若集合有数据使用JRBeanCollectionDataSource
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, new JRBeanCollectionDataSource(list));

        //4.导出文件
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    @RequestMapping(value="exportPdf5",name = "pdf下载")
    public void fillChartsList(HttpServletResponse response) throws JRException, IOException {
        //1.加载jasper文件
        InputStream is = session.getServletContext().getResourceAsStream("/jasper/fillCharts.jasper");

        //2.准备数据
        Map map = new HashMap();

        List<ChartsVo> list = new ArrayList<>();
        list.add(new ChartsVo("皮衣",1000));
        list.add(new ChartsVo("皮裤",1500));
        list.add(new ChartsVo("皮帽",3000));

        //3.填充数据
        //map:填充一个对象,  JRDataSource:填充集合;若集合没有数据使用JREmptyDataSource;若集合有数据使用JRBeanCollectionDataSource
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, new JRBeanCollectionDataSource(list));

        //4.导出文件
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    @RequestMapping(value="exportPdf4",name = "pdf下载")
    public void fillGroupList(HttpServletResponse response) throws JRException, IOException {
        //1.加载jasper文件
        InputStream is = session.getServletContext().getResourceAsStream("/jasper/fillGroup.jasper");

        //2.准备数据
        Map map = new HashMap();

        List<UserVo> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            list.add(new UserVo(i+"姨子","女",18+i,"李玮皮革厂"));
        }

        for (int i = 0; i < 8; i++) {
            list.add(new UserVo(i+"舅子","男",18+i,"小包皮革厂"));
        }

        for (int i = 0; i < 10; i++) {
            list.add(new UserVo(i+"舅子姨子","男女",18+i,"博洋皮革厂"));
        }

        //3.填充数据
        //map:填充一个对象,  JRDataSource:填充集合;若集合没有数据使用JREmptyDataSource;若集合有数据使用JRBeanCollectionDataSource
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, new JRBeanCollectionDataSource(list));

        //4.导出文件
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    @RequestMapping(value="exportPdf3",name = "pdf下载")
    public void fillList(HttpServletResponse response) throws JRException, IOException {
        //1.加载jasper文件
        InputStream is = session.getServletContext().getResourceAsStream("/jasper/fillList.jasper");

        //2.准备数据
        Map map = new HashMap();

        List<UserVo> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            list.add(new UserVo(i+"姨子","女",18+i,"李玮皮革厂"));
        }

        //3.填充数据
        //map:填充一个对象,  JRDataSource:填充集合;若集合没有数据使用JREmptyDataSource;若集合有数据使用JRBeanCollectionDataSource
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, new JRBeanCollectionDataSource(list));

        //4.导出文件
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    @RequestMapping(value="exportPdf2",name = "pdf下载")
    public void fillMap(HttpServletResponse response) throws JRException, IOException {
        //1.加载jasper文件
        InputStream is = session.getServletContext().getResourceAsStream("/jasper/fillMap.jasper");

        //2.准备数据
        Map map = new HashMap();
        map.put("username","李玮");
        map.put("age",19);
        map.put("sex","男");
        map.put("company","李玮皮革厂");

        //3.填充数据
        //map:填充一个对象,  JRDataSource:填充集合;若集合没有数据使用JREmptyDataSource
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, new JREmptyDataSource());

        //4.导出文件
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }


    @RequestMapping(value="exportPdf1",name = "pdf下载")
    public void hello(HttpServletResponse response) throws JRException, IOException {
        //1.加载jasper文件
        InputStream is = session.getServletContext().getResourceAsStream("/jasper/hello.jasper");

        //2.准备数据
        Map map = new HashMap();

        //3.填充数据
        //map:填充一个对象,  JRDataSource:填充集合;若集合没有数据使用JREmptyDataSource
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, new JREmptyDataSource());

        //4.导出文件
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }
}
